package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目: leyou
 *
 * @author 岩冰
 * @version 2018/10/20 11:21
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;
    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts() {
        List<Cart> carts=this.cartService.queryCarts();
        if (carts==null||carts.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        return ResponseEntity.ok(carts);
    }

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        this.cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    @PostMapping("combine")
    public ResponseEntity<Void> combineCarts(@RequestBody Cart[]carts) {
        this.cartService.combineCarts(carts);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Void> updateCarts(@RequestParam("skuId")Long skuId,@RequestParam("num")Integer num) {
        this.cartService.updateCarts(skuId,num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteCarts(@PathVariable("id")String id) {
        this.cartService.deleteCarts(id);
        return ResponseEntity.ok().build();
    }
}
